package com.jmlee.controller;

import com.jmlee.pojo.CommonPage;
import com.jmlee.pojo.CommonResult;
import com.jmlee.pojo.Menu;
import com.jmlee.service.MenuService;
import com.jmlee.service.RoleService;
import com.jmlee.util.JSONUtil;
import com.jmlee.util.SystemDateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleService roleService;


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 用户拥有的菜单信息
     * @param rId
     * @return
     */
    @GetMapping("/roleHoldMenu")
    @ResponseBody
    public String getMenu(String rId) {
        List<Object> allMenuInfo = menuService.getMenuInfoByRid(Long.valueOf(rId));
        return JSONUtil.objectToJson(allMenuInfo);
    }

    /**
     * 获取父菜单JSON
     * @return
     */
    @GetMapping("getAllParentMenu")
    @ResponseBody
    public CommonResult getAllParentMenuList(){
//        List<Object> menuJson = new LinkedList<>();
        List<Menu> pMenuList = menuService.getAllParentMenuList();
//        getJsonModel(menuJson, pMenuList)
        return CommonResult.success(pMenuList,"200");
    }

    /**
     * 获取给穿梭框解析的子菜单JSON
     * @return
     */
    @GetMapping("getAllChildMenu")
    @ResponseBody
    public String getAllChildMenuList(){
        List<Object> menuJson = new LinkedList<>();
        List<Menu> sMenuList = menuService.getAllChildMenu();
        return getJsonModel(menuJson, sMenuList);
    }

    /**
     * 获取JSON模板
     * @param menuJson
     * @param sMenuList
     * @return
     */
    private String getJsonModel(List<Object> menuJson, List<Menu> sMenuList) {
        for (Menu menu: sMenuList) {
            Map<String,Object> menuMap = new LinkedHashMap<>();
            menuMap.put("value",menu.getId().toString());
            menuMap.put("title",menu.getTitle());
            if(menu.getId() == 3L){
                menuMap.put("disabled", true);
            }
            menuJson.add(menuMap);
        }
        return  JSONUtil.objectToJson(menuJson);
    }

    /**
     * 无权限父菜单ID
     * @param rId
     * @return
     */
    @GetMapping("getParentUnable")
    @ResponseBody
    public String getParentUnableId(String rId){
        return JSONUtil.objectToJson(menuService.getUnableListId(Long.valueOf(rId),0L));
    }

    /**
     * 无权限子菜单ID
     * @param rId
     * @return
     */
    @GetMapping("getChildUnable")
    @ResponseBody
    public String getChildUnableId(String rId){
        return JSONUtil.objectToJson(menuService.getUnableListId(Long.valueOf(rId),1L));
    }

    /**
     * 新建菜单
     * @param menu
     * @return
     */
    @PostMapping("/createNewMenu")
    @ResponseBody
    public String createNewMenu(@RequestBody Menu menu){
        menu.setCreateTime(SystemDateUtil.getDaDate());
        String status = menuService.addMenu(menu).toString();
        return status;
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @PostMapping("editMenu")
    @ResponseBody
    public String editMenu(@RequestBody Menu menu){
        String status = menuService.editMenu(menu).toString();
        return status;
    }

    /**
     * 逻辑删除菜单
     * @param menu
     * @return
     */
    @PostMapping("delMenu")
    @ResponseBody
    public String delMenu(@RequestBody Menu menu){
        menu.setStatus(2);
        String status = menuService.editMenu(menu).toString();
        return status;
    }

    /**
     * 权限编辑
     * @return
     */
    @PostMapping("authorityEdit")
    @ResponseBody
    public String editAuthorityByRid(String rId,String enableId){
        String status = "";
        List<String> enableIdList = JSONUtil.jsonToList(enableId,String.class);
        List<Long> allMenuIdList = menuService.allMenuId();

        for (Long id:allMenuIdList) {
           status = roleService.removeAuthority(Long.valueOf(rId),id).toString();
        }
        for (String id:enableIdList) {
          status = roleService.addAuthority(Long.valueOf(rId),Long.valueOf(id)).toString();
        }
        return status;
    }

    /**
     * 获取未被逻辑删除的菜单
     * @param curPage
     * @param pageSize
     * @return
     */
    @PostMapping("menusList")
    @ResponseBody
    public CommonResult getAllMenuList(String curPage, String pageSize, Menu menu) {
    CommonPage commonPage = new CommonPage();
        if (menu == null) {
            menu = new Menu();
        }
        commonPage.setData(menuService.allMenuList(new RowBounds((Integer.parseInt(curPage) - 1) * Integer.parseInt(pageSize), Integer.parseInt(pageSize)), menu));
    commonPage.setCount(menuService.menuCount(menu));
    return CommonResult.success(commonPage,"success");
}

    /**
     * 权限管理
     * @return
     */
    @GetMapping("authority")
    public String authority(){
        return "authority";
    }
    /**
     * 角色管理
     * @return
     */
    @GetMapping("roleManager")
    public String roleManager(){
        return "roleManager";
    }
    /**
     * 菜单管理
     * @return
     */
    @GetMapping("menuManager")
    public String menuManager(){
        return "menuManager";
    }


}
